/**
 * 
 */
package com.shuqi.activity.viewport;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * 搜索模块listview
 * 
 * @author linyuren
 * 
 */
public class SearchListView extends ListView {

    private final int mTouchSlop;

    public SearchListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        final ViewConfiguration configuration = ViewConfiguration.get(context);
        mTouchSlop = configuration.getScaledTouchSlop();
    }

    private OnCloseAndOpenListener listener;
    private boolean isTouch = false;
    private boolean isScroll = false;
    private boolean isHotCmt = false;
//    private boolean isFirstItemWhenOnTouch = false; // 为了控制是否滑到listview顶部才弹出搜索框
    private boolean onlyOnce = false;// 设置打开关闭SearchBox只执行一次

    public void setOnCloseAndOpenListener(OnCloseAndOpenListener listener) {
        this.listener = listener;
    }

    private float onTouchLastY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouch = true;
                onTouchLastY = event.getY();
//                if (getFirstVisiblePosition() == 0) {
//                    isFirstItemWhenOnTouch = true;
//                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                isTouch = false;
//                isFirstItemWhenOnTouch = false;
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                if (getFirstVisiblePosition() == 0) {
//                    isFirstItemWhenOnTouch = true;
//                }
                onlyOnce = false;
                isTouch = true;
                onTouchLastY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float y = event.getY() - onTouchLastY;
                if (!onlyOnce) {
                    if (y > mTouchSlop /*&& isFirstItemWhenOnTouch*/) { //注释掉即为向下滑动就弹出搜索框，打开的话listview滑到顶部才弹出搜索框
                        if (listener != null && listener.open()) {
                            onTouchLastY = event.getY();
                            onlyOnce = true;
                        }
                    } else if (y < -mTouchSlop) {
                        if (listener != null && listener.close()) {
                            onTouchLastY = event.getY();
                            onlyOnce = true;
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                isTouch = false;
//                isFirstItemWhenOnTouch = false;
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void setOnScrollListener(final OnScrollListener listener) {

        super.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (OnScrollListener.SCROLL_STATE_FLING == scrollState
                        || OnScrollListener.SCROLL_STATE_TOUCH_SCROLL == scrollState) {// 滑动状态
                    isScroll = true;
                } else {
                    isScroll = false;
                }
                listener.onScrollStateChanged(view, scrollState);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                listener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        });
    }

    public boolean isShowHotCmt() {
        return !isScroll && !isTouch && (getFirstVisiblePosition() < 3);
    }

    public boolean isHotCmt() {
        return isHotCmt;
    }

    public void setHotCmt(boolean isHotCmt) {
        this.isHotCmt = isHotCmt;
    }

    /** 需要打开或关闭SearchBox时调用 */
    public interface OnCloseAndOpenListener {
        boolean open();

        boolean close();
    }

}
